"""
文字识别
"""
import os
import sys
from typing import List
from alibabacloud_ocr_api20210707.client import Client as ocr_api20210707Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_darabonba_stream.client import Client as StreamClient
from alibabacloud_ocr_api20210707 import models as ocr_api_20210707_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
import json

from .configParser import ConfigParser


class ImageOcr:
    access_key_id = ""
    access_key_secret = ""

    def __init__(self):
        con = ConfigParser()
        self.access_key_id = con.get_access_key_id()
        self.access_key_secret = con.get_access_key_secret()

    def ocr(self, **kwargs):
        # 取参
        url = kwargs.get("url")

        client = open_api_models.Config(
            access_key_id=self.access_key_id,
            access_key_secret=self.access_key_secret
        )
        client.endpoint = f'ocr-api.cn-hangzhou.aliyuncs.com'
        client = ocr_api20210707Client(client)
        recognize_general_request = ocr_api_20210707_models.RecognizeGeneralRequest(
            url=url
        )
        runtime = util_models.RuntimeOptions()
        result = client.recognize_general_with_options(recognize_general_request, runtime)
        result = result.body.data
        result = json.loads(result)
        return result["content"].split(" ")

    def test(self):
        url = "https://jsy.unicorn-go.com/static/media/00931a661b6711efaa700da4d3710d1a.jpg"
        return self.ocr(url=url)


if __name__ == '__main__':
    ImageOcr().ocr(url="https://jsy.unicorn-go.com/static/media/00931a661b6711efaa700da4d3710d1a.jpg")
